<?php
/**
 * 从决策系统中间表取存销比数据
 * @author sundongdong
 * @date 2016-09-27
 */
class Cron_Model_Decision_SaleStockPre extends Model_Base {
	private $db = '';
	private $host_db = '';
	
	public function __construct(){
		$this->db = $this->db('dms_db');
		$this->host_db = $this->db('write_db');
	}
	/**
	 * 获取总数
	 */
	public function getAccout() {
		return $this->db->fetchOne("SELECT count(*) from public_dm_sale_stock_pre");
	}
	/**
	 * 分批获取数据
	 * @param number $start
	 * @param number $limit
	 */
	public function getData($start = 0,$limit = 1000) {
		$sql = "SELECT * FROM public_dm_sale_stock_pre LIMIT ".$start.",".$limit;
		return $this->db->fetchAll($sql);
	}
	/**
	 * 查看产品库存表中是否存在数据
	 * @param unknown $goods_sn
	 * @param unknown $warehouse_code
	 * @return string
	 */
	public function existStockData($goods_sn,$warehouse_code) {
		$sql = "SELECT id from p_product_warehouse_stock where goods_sn=:goods_sn and warehouse_code = :warehouse_code";
		return $this->host_db->fetchOne($sql,['goods_sn'=>$goods_sn,'warehouse_code'=>$warehouse_code]);
	}
	/**
	 * 更新产品库存表
	 * @param unknown $sale_stock_pre
	 * @param unknown $id
	 */
	public function updateStock($sale_stock_pre, $id) {
		try {
			$this->host_db->update('p_product_warehouse_stock', ['sale_stock_pre'=>$sale_stock_pre], 'id='.$id);
		} catch (Exception $e) {
			throw $e;
		}
	}
}